package zhc.app.controller;


import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import zhc.app.base.aop.slog.SLog;
import zhc.app.entity.Sys_unit;
import zhc.app.service.SysUnitService;
import zhc.framework.base.Result;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@IocBean
@At("/platform/sys/unit")
public class SysUnitController {
    private static final Log log = Logs.get();
    @Inject
    private SysUnitService sysUnitService;

    @At("")
    @Ok("beetl:/platform/sys/unit/index.html")
    //@RequiresPermissions("sys.manager.unit")
    public Object index() {
        long d1=System.currentTimeMillis();
        Object obj=sysUnitService.query(Cnd.where("parentId", "is", null).asc("path"));
        System.out.println("查询时间:"+(System.currentTimeMillis()-d1));
        return obj;
    }

    @At
    @Ok("beetl:/platform/sys/unit/add.html")
    //@RequiresPermissions("sys.manager.unit")
    public Object add(@Param("pid") Long pid) {
        return pid==null ? null : sysUnitService.fetch(pid);
    }

    @At
    @Ok("json")
    //@RequiresPermissions("sys.manager.unit.add")
    @SLog(tag = "新建单位", msg = "单位名称:${args[0].name}")
    public Object addDo(@Param("..") Sys_unit unit, @Param("parentId") Long parentId, HttpServletRequest req) {
        try {
            sysUnitService.save(unit, parentId);
            return Result.success("system.success");
        } catch (Exception e) {
            return Result.error("system.error");
        }
    }

    @At("/child/?")
    @Ok("beetl:/platform/sys/unit/child.html")
    //@RequiresPermissions("sys.manager.unit")
    public Object child(Long id) {
        return sysUnitService.query(Cnd.where("parentId", "=", id).asc("path"));
    }

    @At("/detail/?")
    @Ok("beetl:/platform/sys/unit/detail.html")
    //@RequiresPermissions("sys.manager.unit")
    public Object detail(Long id) {
        return sysUnitService.fetch(id);
    }

    @At("/edit/?")
    @Ok("beetl:/platform/sys/unit/edit.html")
    //@RequiresPermissions("sys.manager.unit")
    public Object edit(Long id, HttpServletRequest req) {
        Sys_unit unit = sysUnitService.fetch(id);
        if (unit != null) {
            if(unit.getParentId()!=null){
                req.setAttribute("parentUnit", sysUnitService.fetch(unit.getParentId()));
            }else{
                req.setAttribute("parentUnit", null);
            }
        }
        return unit;
    }

    @At
    @Ok("json")
    //@RequiresPermissions("sys.manager.unit.edit")
    //@SLog(tag = "编辑单位", msg = "单位名称:${args[0].name}")
    public Object editDo(@Param("..") Sys_unit unit, @Param("parentId") Long parentId, HttpServletRequest req) {
        try {
           // unit.setOpBy(StringUtil.getUid());
            unit.setOpAt(System.currentTimeMillis() / 1000);
            sysUnitService.updateIgnoreNull(unit);
            return Result.success("system.success");
        } catch (Exception e) {
            return Result.error("system.error");
        }
    }

    @At("/delete/?")
    @Ok("json")
    //@RequiresPermissions("sys.manager.unit.delete")
    //@SLog(tag = "删除单位", msg = "单位名称:${args[1].getAttribute('name')}")
    public Object delete(Long id, HttpServletRequest req) {
        try {
            Sys_unit unit = sysUnitService.fetch(id);
            req.setAttribute("name", unit.getName());
            if ("0001".equals(unit.getPath())) {
                return Result.error("system.not.allow");
            }
            sysUnitService.deleteAndChild(unit);
            return Result.success("system.success");
        } catch (Exception e) {
            return Result.error("system.error");
        }
    }

    @At
    @Ok("json")
    //@RequiresPermissions("sys.manager.unit")
    public Object tree(@Param("pid") Long pid) {
        String op=pid==null?"is":"=";
        List<Sys_unit>  list = sysUnitService.query(Cnd.where("parentId", op, pid).asc("path"));
        List<Map<String, Object>> tree = new ArrayList();
        for (Sys_unit unit : list) {
            Map<String, Object> obj = new HashMap();
            obj.put("id", unit.getId());
            obj.put("text", unit.getName());
            obj.put("children", unit.isHasChildren());
            tree.add(obj);
        }
        return tree;
    }
}
